#Display warnings (-w for unix in the header "#!/usr/bin/perl -w")
use warnings;


use strict;

#This is for current directory
use Cwd;

#This is a subroutine called "greeting"
sub greeting {
	print "This is a sub: Hello World!\n";
}

#Executing a subroutine
&greeting;

#A few print statements
print "Hello World!\n";
print "The answer is  = ", (1+2+3), " and the right answer is ", 4/(3 + 2)*4/2 - 5;

#Print current directory
print "The current directory is ", cwd(), "\n";

#Print an octal number (preceeded by a '0')
print "'10' in octal is ", 010, "\n";

#Print a hexadecimal number (preceeded by a 0x)
print "'0xABCD123' in hex is ", 0xABCD123, "\n";

#Printing lists
print 123, 456, 789, "\n";
print "Hello", "World", "\n";
print "These is a list of ", 123, " mixed variables\n";

#Initializing and printing a variable (scalar)
my $testvar1 = 30; #or can be alternatively declared as: my $testvar; $testvar = 30;
print "The value of the test variable 'testvar' is $testvar1\n";
my $testvar5 = "1 2 3 4 5 6";			# String variable.
print ($testvar5, "\n");

#Accessing list elements
my $testvar2 = 2;
print qw(This is a sentence.)[$testvar2], "\n";
print qw(This is a sentence.)[-1], "\n";
print qw(This is a sentence.)[-2], "\n";
print ((1,2,3,4,5,6,7,8,9)[3,5,7], "\n");
my $testvar3; my $testvar4;
($testvar3, $testvar4) = (1,3);
print $testvar3, " and ", $testvar4, "\n";

#Ranges
print ((1..6), "\n");
print ((1.4..7.8), "\n");
print (reverse(-3 .. 6), "\n");
print (reverse(qw(This is a reverse sentence)), "\n");
print ((qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[-2..3]), "\n");


#Arrays
my @days;
@days = qw(Tue Wed Thu Fri);
print ($days[2], "\n");				# Used as a scalar.
print (@days[1..3], "\n");			# Used as an array.
my $oneDay = @days; 				# The scalar variable 'oneDay' captures the size of the array 'days'.
print ("The size of the array 'days' is ", $oneDay, "\n");
print ("The days printed with NO spaces in between are ", @days, ".\n");	# Prints without spaces.
print ("The days printed with spaces in between are @days.\n");			# Prints with space.
my @stringArray = ("This is a number ", $testvar5, " series");
print (@stringArray, "\n");
my @mixedArray = ("This is a mixed array ", -2, -1, 0, $testvar5, 7, 8, 9);
print (@mixedArray, "\n");
@days = (qw(Mon), @days, qw(Sat Sun));		#Adding elements to an array.
print("@days\n");

#STDIN
print "Enter a number: ";
#my $input = <STDIN>;
#sleep 2;					#pause for 2 seconds
#print ("The number you entered is $input\n");

#for loop
for $oneDay (@days)
{
	print $oneDay, "\n";
}

#The above is similar to
for (@days)
{
	print $_, "\n";
}

#The above is still similar to
for (0..$#days) 				#This is a way to index the array @days with $_.
{
	print $days[$_], "\n";
}

#Convert data types
print hex("34"), "\n";


#Bitwise operators
print 34 & 52, "\n";				# AND
print 34 | 52, "\n";				# OR
print 34 ^ 52, "\n";				# XOR
print ~52, "\n";				# NOT

#Repetetion operator
print  "GO!" x$#days, "\n";

#chomp
print "Enter a string: ";
my $input = "asdf";
print "The string you entered is $input $input $input Notice the endline in your input\n";
chomp($input);
print "The string you entered is $input $input $input Notice the endline in your input\n";

##########################
#Connecting to SQL Server#
##########################

use DBI;
use DBD::ODBC;
print "ODBC Version = $DBD::ODBC::VERSION\n";
my @drivers = DBI-> installed_versions;
print join(", ", @drivers), "\n";

# List all dsn odbc drivers.
my @dsns = DBI->data_sources('ODBC');
foreach my $d (@dsns)
{
  print "$d\n";
}

# Set connection parameters
my $dsn = 'dbi:ODBC:NTDBDriver';
#my $host = '10.50.1617.0';
#my $database = 'NTDB';
my ($username, $password) = ('trauma', 'trauma');
print "username = $username, password = $password\n";
my $ntdb = DBI -> connect($dsn,$username,$password);

if (!defined($ntdb))
{
    print "Error connecting to DSN '$dsn'\n";
    print "Error was:\n";
    print "$DBI::errstr\n";         # $DBI::errstr is the error received from the SQL server
}
else
{
    print "Connection successful!\n";	
}

# Writing, executing sql queries
my $ColumnName = "EDGCSEYE";
my $sql = "SELECT TOP 10 $ColumnName FROM RDS_ED";
print "$sql\n";

my $sth = $ntdb -> prepare($sql);
$sth -> execute();

my ($EDGCSEYE, @EDGCSEYE1, @EDGCSEYE2, @EDGCSEYE3, @EDGCSEYE4, $iter);
$iter = 0;
while(($EDGCSEYE) = $sth -> fetchrow_array())
{
	if(!defined($EDGCSEYE))
	{
		print "not defined\n";
	}
	elsif($EDGCSEYE == 1)
	{
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		print "$EDGCSEYE\n";
	}
	elsif($EDGCSEYE == 2)
	{
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		print "$EDGCSEYE\n";
	}
	elsif($EDGCSEYE == 3)
	{
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		print "$EDGCSEYE\n";
	}
	elsif($EDGCSEYE == 4)
	{
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		$EDGCSEYE1[$iter] = 0;
		print "$EDGCSEYE\n";
	}
	$iter++;
}
